const Toast = {
  init({
    type="default",
    position="top",
    title="",
    closeType="auto",
    delayTime=2000,
    close
  }){
    this.type = type;
    this.position = position;
    this.title = title;
    this.closeType = closeType;
    this.delayTime = delayTime;
    this.close = close;
    // 创建弹出框元素，设置内容，插入到页面
    this.createEle();
    // 检测关闭方式
    if(this.closeType === "click"){
      this.ele.onclick = function(){
        this.remove();
        close && close();
      }
    }else{
      setTimeout(()=>{
        this.ele.remove();
        close && close();
      }, this.delayTime)
    }
  },
  createEle(){
    this.ele = document.createElement("dialog");
    this.ele.innerText = this.title;
    document.body.appendChild(this.ele);
    this.ele.style.display = "block";
    this.ele.style.position = "fixed";
    this.ele.style.color = this.getColor();
    this.ele.style.top = this.getPos() + "px";
  },
  getColor(){
    switch(this.type){
      case "success":
        return "green";
      case "error":
        return "red";
      case "warning":
        return "yellow";
      case "info":
        return "blue";
      case "default":
        return "#ccc";
      default:
        return "#ccc";
    }
  },
  getPos(){
    switch(this.position){
      case "top":
        return 0;
      case "center":
        return (document.documentElement.clientHeight - this.ele.offsetHeight) / 2;
      case "bottom":
        return document.documentElement.clientHeight - this.ele.offsetHeight;
      default:
        return 0;
    }
  }
}

export default Toast.init.bind(Toast);